﻿using Newtonsoft.Json;
using ScreenMonitor.Web.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace ScreenMonitor.Web.Ajax
{
    /// <summary>
    /// OperateAshxBase 的摘要说明
    /// </summary>
    public abstract class JsonOperateAshxBase : IHttpHandler, IRequiresSessionState
    {
        private HttpContext NowContext;

        public void ProcessRequest(HttpContext context)
        {

            NowContext = context;
            ResponceObject ResObj = new ResponceObject();

            StreamReader sr = new StreamReader(context.Request.GetBufferedInputStream());
            string JsonData = sr.ReadToEnd();

            //if (IsNeedUserLogin())
            //{
            //    if (context.Session["LoginedUser"] == null)
            //    {
            //        ReObj.IsSuccess = false;
            //        ReObj.Message = "未登录用户！";
            //        goto EndOP;
            //    }
            //}

            try
            {
                RequestObject ReqObj = JsonConvert.DeserializeObject<RequestObject>(JsonData);
                this.ProcessRequest(ReqObj, ResObj);
            }
            catch(Exception ex)
            {
                ResObj.IsSuccess = false;
                ResObj.Message = "服务端出错：" + ex.Message;
            }

            //EndOP:
            context.Response.Write(JsonConvert.SerializeObject(ResObj));
            NowContext = null;

        }

        public abstract void ProcessRequest(RequestObject ReqObj, ResponceObject ResObj);

        public abstract bool IsNeedUserLogin();

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}